﻿using CefSharp;
using Coolvis.Component;
using CPPEI.Coolzon.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Coolvis.PartLib.Default
{
    /// <summary>
    /// Interaction logic for WebView.xaml
    /// </summary>
    [XmlTag("webview")]
    public partial class WebView : ControlPart
    {
        private static CefSettings s_settings;

        private string _url;

        [XProperty(XPropertyCategory.Other, "Url", "Url地址")]
        [XmlProp("url")]
        public string Url
        {
            get
            {
                return _url;
            }
            set
            {
                if (_url != value)
                {
                    _url = value;
                    ReLoadPage();
                }
            }
        }

        private CefSharp.Wpf.ChromiumWebBrowser _webView;
        public WebView()
        {
            InitializeComponent();

            if (s_settings == null)
            {
                s_settings = new CefSettings();
                s_settings.SetOffScreenRenderingBestPerformanceArgs();
                Cef.Initialize(s_settings, true, false);
            }
            _webView = new CefSharp.Wpf.ChromiumWebBrowser();
            _webView.Background = Brushes.Black;
            
            this.Content = _webView;
            _webView.FrameLoadEnd += _webView_FrameLoadEnd;
        }

        void _webView_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
            //try
            //{
            //    // 0.3333 = -6.03
            //    if (_scaleX != 1.0d || _scaleY != 1.0d)
            //    {

            //        this.Dispatcher.BeginInvoke(new Action(() =>
            //        {
            //            if (_scaleX == 0.333333)
            //            {
            //                _webView.SetZoomLevel(-6.02);
            //            }
            //            else if (_scaleX == 0.666666)
            //            {
            //                _webView.SetZoomLevel(-2.19);
            //            }
            //            else
            //            {
            //                _webView.SetZoomLevel(0.0);
            //            }
            //        }));
            //    }
            //    else
            //    {
            //        this.Dispatcher.BeginInvoke(new Action(() =>
            //        {
            //            _webView.SetZoomLevel(0);
            //        }));
            //    }
            //}
            //catch (Exception ex)
            //{
            //}
        }

        protected override void OnActive(PartContext context)
        {
            if(IsFirstActive)
            {
                /*
                Uri uri = new Uri(@"pack://application:,,,/Coolzon.PartLib.ECharts;component/Resources/mapchina.html");
                //using (Stream source = Application.GetResourceStream(uri).Stream)
                //{
                //    this.ctlWebBrowser.NavigateToStream(source);
                //}
                Stream source = Application.GetResourceStream(uri).Stream;
                this.ctlWebBrowser.NavigateToStream(source);
                */
                if (!string.IsNullOrEmpty(Url))
                {
                    _webView.RegisterJsObject("CZScene", this.Scene.GetScriptObj());
                    if (Url.ToUpper().StartsWith("HTTP://")
                        || Url.ToUpper().StartsWith("HTTPS://"))
                    {
                        _webView.Address = Url;
                    }
                    else
                    {
                        _webView.Address = SolutionResourceManager.GetAbsoluteFilePath(Url);
                    }
                }
            }
            base.OnActive(context);
        }

        protected override void OnDestory()
        {
            _webView.Dispose();
            base.OnDestory();
        }

        public override void RaiseScriptNetEvent(string eventName, string dataString)
        {
            try
            {
                _webView.ExecuteScriptAsync("jzcode.event.callback", eventName, dataString);
                return;
            }
            catch (Exception ex)
            {
            }

            try
            {
                _webView.ExecuteScriptAsync("civs.event.callback", eventName, dataString);
                return;
            }
            catch (Exception ex)
            {
            }
        }

        // 重新加载页面
        private void ReLoadPage()
        {
            if (!string.IsNullOrEmpty(Url))
            {
                //_webView.RegisterJsObject("CZScene", this.Scene.GetScriptObj());
                if (Url.ToUpper().StartsWith("HTTP://")
                    || Url.ToUpper().StartsWith("HTTPS://"))
                {
                    _webView.Address = Url;
                }
                else
                {
                    _webView.Address = SolutionResourceManager.GetAbsoluteFilePath(Url);
                }
            }
        }
    }
}
